package difangame.level;

/**
 * @author kathleen
 */

import java.awt.Image;
import java.awt.Point;
import difangame.Constants;
import difangame.sprite.Bug;
import difangame.sprite.Stone;
import framework.Level;
import framework.Sprite;


public abstract class PlayableLevel extends Level {

    protected Bug myPlayer;
    protected String myPlayerType = Constants.bug;
    private Image mySplash = null;
    protected final Point[] pos = { new Point(150, 335), new Point(200, 300), new Point(100, 265),
                                   new Point(150, 230), new Point(120, 195), new Point(200, 160),
                                   new Point(100, 125) };
    private final Point starPosition = new Point(150, 200);
    private Sprite myGoalObject = null;

    @Override
    protected void initializeLevel () {
        mySplash = super.getMyGame().loadImage("background.png");
    }

    @Override
    protected int testWinCondition () {
        if (myPlayer.hasCarrier() && myPlayer.matchesMyCarrierName(Constants.star)) {
            return getNextLevelIfWin();
        }
        return -1;
    }

    @Override
    protected int testLoseCondition () {
        if (!myPlayer.hasCarrier()) {
            return getNextLevelIfLose();
        }
        return -1;
    }

    @Override
    protected void levelBehavior () {
        for (Sprite s : getMySprites().get(Constants.stone)) {
            Stone st = (Stone) s;
            st.setPrizeStatus(false);
        }
    }

    @Override
    protected void loadLevelObjects () {
        for (int i = 0; i < 5; i++) {
            loadSprite(Constants.stone, pos[i]);
        }
        loadSprite(myPlayerType, pos[0]);
        setPlayer(getMySprites().get(myPlayerType).get(0));

        loadSprite(Constants.star, starPosition);
        myGoalObject = getMySprites().get(Constants.star).get(0);
        loadCollision(Constants.bugStone);
        loadCollision(Constants.bugStar);
    }

    protected void setPlayer (Sprite s) {
        Bug mySprite = (Bug) s;
        myPlayer = mySprite;
        myPlayer.setMyCarrier(getMySprites().get(Constants.stone).get(0));
    }

    protected void setMyGoalObjectPosition (Point p) {
        if (myGoalObject != null) {
            myGoalObject.setMyCenter(p);
        }
    }

    abstract protected int getNextLevelIfWin ();

    abstract protected int getNextLevelIfLose ();

}
